<!doctype html>
<html>
<head>
    <script src="../../build/output/playcanvas-latest.js"></script>
    <link href="../style.css" rel="stylesheet" />
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- The script -->
    <script>
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas);
        app.start();

        // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
        app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
        app.setCanvasResolution(pc.RESOLUTION_AUTO);

        app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);

        // Create a Entity with a Box model component
        var box = new pc.Entity();
        box.addComponent("model", {
            type: "box",
        });

        // Create an Entity with a point light component and a sphere model component.
        var light = new pc.Entity();
        light.addComponent("light", {
            type: "point",
            color: new pc.Color(1, 0, 0),
            radius: 10
        });
        light.addComponent("model", {
            type: "sphere"
        });
        // Scale the sphere down to 0.1m
        light.setLocalScale(0.1, 0.1, 0.1);

        // Create an Entity with a camera component
        var camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0.4, 0.45, 0.5)
        });

        // Add the new Entities to the hierarchy
        app.root.addChild(box);
        app.root.addChild(light);
        app.root.addChild(camera);

        // Move the camera 10m along the z-axis
        camera.translate(0, 0, 10);

        // Set an update function on the app's update event
        var angle = 0;
        app.on("update", function (dt) {
            angle += dt;
            if (angle > 360) {
                angle = 0;
            }

            // Move the light in a circle
            light.setLocalPosition(3 * Math.sin(angle), 0, 3 * Math.cos(angle));

            // Rotate the box
            box.setEulerAngles(angle*2, angle*4, angle*8);
        });

        // load a texture, create material and apply to box
        app.assets.loadFromUrl("../assets/clouds.jpg", "texture", function (err, asset) {
            var material = new pc.PhongMaterial();
            material.diffuseMap = asset.resource;
            material.update();

            box.model.model.meshInstances[0].material = material;
        });
    </script>
</body>
</html>
